﻿using System;
using System.Text;

namespace FuzzScript
{
    class FuzzyPredicate: Node, IDOMValueNode
    {

        private FuzzyPredicate() { }

        internal FuzzyPredicate(string p_sName)
        {
            m_SiblingNodes.Add(null);

            m_sName = p_sName;

            SymbolTable.Instance.Put(SymbolTable.AttributePredicateValue(m_sName), this);

        }



        internal INode Expression
        {
            get { return m_SiblingNodes[0]; }
            set { m_SiblingNodes[0] = value; }
        }

        public override string ToString()
        {
            return "PREDICATE " + m_sName + "{" + Expression.ToString() + "}";
        }


        private double m_dValue = 0;

        public double DOMValue
        {
            get { return m_dValue; }
        }

        public override double Execute()
        {
            m_dValue = Expression.Execute();

            #if DEBUG
            System.Console.WriteLine(m_sName + ":" + m_dValue);
            #endif

            return 0;
        }

        public override void AddSibling(INode p_SiblingNode)
        {
            throw new NotImplementedException();
        }

        public override string Generate()
        {
            StringBuilder sCode = new StringBuilder();

            sCode.Append("private double ");
            sCode.Append(SymbolTable.AttributePredicateValue(m_sName));
            sCode.Append(";\nprivate double ");
            sCode.Append(SymbolTable.MethodPredicate(m_sName));
            sCode.Append("()\n");
            sCode.Append("{");
            sCode.Append("return ");
            sCode.Append(this.Expression.Generate());
            sCode.Append(";}\n");

            return sCode.ToString();
        }
    }
}
